package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;

import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);
//        //要使用这个结果集的品牌id
//        ArrayList<Long> tmIdList = new ArrayList<>();
//        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
//            baseCategoryTrademarkList.forEach(baseCategoryTrademark -> {
//                Long trademarkId = baseCategoryTrademark.getTrademarkId();
//                tmIdList.add(trademarkId);
//            });
//            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(tmIdList);
//            return baseTrademarkList;
//        }
//        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
//            List<Long> tradeMarkIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
//                return baseCategoryTrademark.getTrademarkId();
//            }).collect(Collectors.toList());
//            return baseTrademarkMapper.selectBatchIds(tradeMarkIdList);
//        }
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            List<Long> tradeMarkIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            return baseTrademarkMapper.selectBatchIds(tradeMarkIdList);
        }
        return null;
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //获取已绑定的品牌id 1 3
            List<Long> tradeMarkIdList = baseCategoryTrademarkList.stream()
                    .map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //查询所有品牌数据 然后去重  map：映射关系
            //参数类型是：集合对象的泛型
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null).stream().filter(baseTrademark -> {
                return !tradeMarkIdList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
            return baseTrademarkList;
        }
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //获取要绑定的id
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //第一种方式:循环遍历插入
//        if (!CollectionUtils.isEmpty(trademarkIdList)){
//            //循环遍历
//            trademarkIdList.forEach(tmId -> {
//                //创建一个对象
//                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
//                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
//                baseCategoryTrademark.setTrademarkId(tmId);
//                //遍历执行insert语句
//                baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
//            });
//        }
        ArrayList<BaseCategoryTrademark> baseCategoryTrademarkArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(trademarkIdList)){
//            trademarkIdList.forEach(tmId->{
//                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
//                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
//                baseCategoryTrademark.setTrademarkId(tmId);
//                baseCategoryTrademarkArrayList.add(baseCategoryTrademark);
//            });
            List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream().map(tmId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(tmId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());

            this.saveBatch(baseCategoryTrademarkList);
        }
    }

    @Override
    public void removeCategory3AndTmIdIdById(Long category3Id, Long tmId) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        queryWrapper.eq("trademark_id",tmId);
        baseCategoryTrademarkMapper.delete(queryWrapper);
    }
}
